#!/bin/sh

# Slackware build script for SFML (Simple and Fast Multimedia Library)

# Written by Pawel Standowicz (standek@gmail.com)

PRGNAM=SFML
VERSION=${VERSION:-1.6}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# Archive package name suffixes are different depending on system architecture.
ARCHIVESUFFIX=${ARCHIVESUFFIX:-32}	# Default for 32bit systems

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
  ARCHIVESUFFIX="64"		# For 64bit systems
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION-sdk-linux-$ARCHIVESUFFIX.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

# Patch Makefile so that it respects CFLAGS
sed -i "s/CFLAGS\ .*=/CFLAGS +=/" src/SFML/Makefile

# Patch Makefile to put libs in correct place
sed -i "s|(DESTDIR)/lib|(DESTDIR)/lib$LIBDIRSUFFIX|" src/SFML/Makefile

# Add a big patch for gcc-4.7.x
patch -p1 < $CWD/gcc47.patch

CFLAGS="$SLKCFLAGS" \
  make
make install DESTDIR=$PKG/usr

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -ar \
  doc/* \
  $PKG/usr/doc/$PRGNAM-$VERSION

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
